package com.ra.statis;

import java.util.Date;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;

import com.googlecode.cswish.annotation.QLFormula;
import com.googlecode.cswish.annotation.cm;
import com.ra.crm.CustomerServiceStaff;
import com.ra.crm.SalerStaff;
import com.ra.landlord.LandlordUser;

@Entity
@cm("房东评估")
public class LandlordEstimation {

	private Integer id;
	
	@cm("比较符合需求")
	public final static Integer FEEDBACK_TYPE_1 = 1;
	@cm("一般符合需求")
	public final static Integer FEEDBACK_TYPE_13 = 13;
	@cm("软件不容易操作")
	public final static Integer FEEDBACK_TYPE_2 = 2;
	@cm("软件功能不适合")
	public final static Integer FEEDBACK_TYPE_3 = 3;
	@cm("信息安全问题")
	public final static Integer FEEDBACK_TYPE_4 = 4;
	@cm("无效用户")
	public final static Integer FEEDBACK_TYPE_5 = 5;
	@cm("其它原因")
	public final static Integer FEEDBACK_TYPE_6 = 6;
	@cm("租客")
	public final static Integer FEEDBACK_TYPE_7 = 7;
	@cm("电话失效")
	public final static Integer FEEDBACK_TYPE_8 = 8;
	@cm("电话无人接听")
	public final static Integer FEEDBACK_TYPE_9 = 9;
	@cm("有其他特殊需求")
	public final static Integer FEEDBACK_TYPE_10 = 10;
	//@cm("分散式管理模式")
	//public final static Integer FEEDBACK_TYPE_11 = 11;
	@cm("单套或少量房源")
	public final static Integer FEEDBACK_TYPE_12 = 12;
	
	@cm("客户")
	public final static Integer GROUP_FEEDBACK_TYPE_CUSTOMER = 1;
	@cm("非客户")
	public final static Integer GROUP_FEEDBACK_TYPE_NONCUSTOMER = 2;
	@cm("未知")
	public final static Integer GROUP_FEEDBACK_TYPE_UNKNOWN = 3;
	
	@cm("房东")
	private LandlordUser landlordUser;
	
	@cm("房源总数评分")
	private Integer scope;
	
	@cm("房源质量评分")
	private Integer qualityScope;
	
	@cm("激活")
	private Boolean active;
	
	// 激活日期
	@cm("评估日期")
	private Date modifyOn;
	
	@cm("丢失")
	private Boolean lose;
	
	@cm("使用PC")
	private Boolean usePC;
	
	@cm("使用APP")
	private Boolean useAPP;
	
	@cm("登录次数")
	private Integer loginCount;
	
	@cm("楼房数")
	private Integer buildingCount;
	
	@cm("房间数")
	private Integer roomCount;
	
	// transient
	@cm("楼房总数")
	private Integer totalBuildingCount;
	
	// transient
	@cm("房间总数")
	private Integer totalRoomCount;
	
	@cm("套间数")
	private Integer suiteCount;
	
	@cm("套间房屋数")
	private Integer suiteRoomCount;
	
	@cm("租客用户数")
	private Integer weixinUserCount;
	
	@cm("合约数")
	private Integer agreementCount;
	
	@cm("发布房源数")
	private Integer publishCount;
	
	@cm("房屋照片数")
	private Integer pictureCount;
	
	@cm("扫码次数")
	private Integer qrCodeCount;
	
	@cm("绑定银行卡")
	private Boolean bindBankCard;
	
	@cm("上次登录时间")
	private Date lastLoginTime;
	
	@cm("分配给维护人员的时间")
	private Date assignDate;
	
	@cm("电话拨打时间")
	private Date phoneDate;
	
	@cm("客户反馈")
	private Integer feedbackType;
	
	@cm("归纳反馈")
	private Integer groupFeedbackType;
	
	@cm("房东使用功能")
	private LandlordFunction landlordFunction;
	
	@cm("备注")
	private String markup;
	
	// CustomerServiceStaff
	@cm("客服人员")
	private CustomerServiceStaff customerService;
	
	@cm("销售人员")
	private SalerStaff saler;
	
	@cm("销售人员2")
	private SalerStaff saler2;
	
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	
	@ManyToOne(fetch=FetchType.LAZY)
	@LazyToOne(LazyToOneOption.PROXY)
	public LandlordUser getLandlordUser() {
		return landlordUser;
	}

	public void setLandlordUser(LandlordUser landlordUser) {
		this.landlordUser = landlordUser;
	}

	public Integer getScope() {
		return scope;
	}

	public void setScope(Integer scope) {
		this.scope = scope;
	}

	public Integer getQualityScope() {
		return qualityScope;
	}

	public void setQualityScope(Integer qualityScope) {
		this.qualityScope = qualityScope;
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	public Boolean getLose() {
		return lose;
	}

	public void setLose(Boolean lose) {
		this.lose = lose;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	@LazyToOne(LazyToOneOption.PROXY)
	public CustomerServiceStaff getCustomerService() {
		return customerService;
	}

	public void setCustomerService(CustomerServiceStaff customerService) {
		this.customerService = customerService;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	@LazyToOne(LazyToOneOption.PROXY)
	public SalerStaff getSaler() {
		return saler;
	}

	public void setSaler(SalerStaff saler) {
		this.saler = saler;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	@LazyToOne(LazyToOneOption.PROXY)
	public SalerStaff getSaler2() {
		return saler2;
	}

	public void setSaler2(SalerStaff saler2) {
		this.saler2 = saler2;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getModifyOn() {
		return modifyOn;
	}

	public void setModifyOn(Date modifyOn) {
		this.modifyOn = modifyOn;
	}

	@Temporal(TemporalType.TIMESTAMP)
	public Date getAssignDate() {
		return assignDate;
	}

	public void setAssignDate(Date assignDate) {
		this.assignDate = assignDate;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	public Date getPhoneDate() {
		return phoneDate;
	}

	public void setPhoneDate(Date phoneDate) {
		this.phoneDate = phoneDate;
	}
	
	public Integer getWeixinUserCount() {
		return weixinUserCount;
	}

	public void setWeixinUserCount(Integer weixinUserCount) {
		this.weixinUserCount = weixinUserCount;
	}

	public Boolean getUsePC() {
		return usePC;
	}

	public void setUsePC(Boolean usePC) {
		this.usePC = usePC;
	}

	public Boolean getUseAPP() {
		return useAPP;
	}

	public void setUseAPP(Boolean useAPP) {
		this.useAPP = useAPP;
	}

	public Integer getLoginCount() {
		return loginCount;
	}

	public void setLoginCount(Integer loginCount) {
		this.loginCount = loginCount;
	}

	@Transient
	@QLFormula(isColumn=true, value="(a.buildingCount+a.suiteCount)", type=Integer.class)
	public Integer getTotalBuildingCount() {
		if (totalBuildingCount == null) {
			if (buildingCount != null || suiteCount != null) {
				totalBuildingCount = toInt(buildingCount) + toInt(suiteCount);
			}
		}
		return totalBuildingCount;
	}
	
	public void setTotalBuildingCount(Integer totalBuildingCount) {
		this.totalBuildingCount = totalBuildingCount;
	}

	private int toInt(Integer i) {
		return i == null ? 0 : i;
	}
	
	public Integer getBuildingCount() {
		return buildingCount;
	}

	public void setBuildingCount(Integer buildingCount) {
		this.buildingCount = buildingCount;
	}

	@Transient
	@QLFormula(isColumn=true, value="(a.roomCount+a.suiteRoomCount)", type=Integer.class)
	public Integer getTotalRoomCount() {
		if (totalRoomCount == null) {
			if (roomCount != null || suiteRoomCount != null) {
				totalRoomCount = toInt(roomCount) + toInt(suiteRoomCount);
			}
		}
		return totalRoomCount;
	}
	
	public void setTotalRoomCount(Integer totalRoomCount) {
		this.totalRoomCount = totalRoomCount;
	}

	public Integer getRoomCount() {
		return roomCount;
	}

	public void setRoomCount(Integer roomCount) {
		this.roomCount = roomCount;
	}

	public Integer getSuiteCount() {
		return suiteCount;
	}

	public void setSuiteCount(Integer suiteCount) {
		this.suiteCount = suiteCount;
	}

	public Integer getSuiteRoomCount() {
		return suiteRoomCount;
	}

	public void setSuiteRoomCount(Integer suiteRoomCount) {
		this.suiteRoomCount = suiteRoomCount;
	}

	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public Integer getFeedbackType() {
		return feedbackType;
	}

	public void setFeedbackType(Integer feedbackType) {
		this.feedbackType = feedbackType;
	}

	@Transient
	@QLFormula(isColumn=false, value=":qlFormulaForGroupFeedbackType")
	public Integer getGroupFeedbackType() {
		if (groupFeedbackType == null) { 
			if (feedbackType != null) {
				if (feedbackType.intValue() == FEEDBACK_TYPE_1 || feedbackType.intValue() == FEEDBACK_TYPE_2|| feedbackType.intValue() == FEEDBACK_TYPE_4 || feedbackType.intValue() == FEEDBACK_TYPE_10) {
					groupFeedbackType = GROUP_FEEDBACK_TYPE_CUSTOMER;
				} else if (feedbackType.intValue() == FEEDBACK_TYPE_3 || feedbackType.intValue() == FEEDBACK_TYPE_7 || feedbackType.intValue() == FEEDBACK_TYPE_8
						|| feedbackType.intValue() == FEEDBACK_TYPE_5 || feedbackType.intValue() == FEEDBACK_TYPE_12) {
					groupFeedbackType = GROUP_FEEDBACK_TYPE_NONCUSTOMER;
				} else {
					groupFeedbackType = GROUP_FEEDBACK_TYPE_UNKNOWN;
				}
			}
		}
		return groupFeedbackType;
	}

	public void setGroupFeedbackType(Integer groupFeedbackType) {
		this.groupFeedbackType = groupFeedbackType;
	}
	
	public static String qlFormulaForGroupFeedbackType(Map<String, Object> model) {
		Object groupFeedbackType = model.get("groupFeedbackType");
		if (groupFeedbackType == null) {
			return "";
		} else {
			Integer theGroupFeedbackType = new Integer(groupFeedbackType.toString());
			if (GROUP_FEEDBACK_TYPE_CUSTOMER.equals(theGroupFeedbackType)) {
				return String.format("(a.feedbackType = %s or a.feedbackType = %s or a.feedbackType = %s or a.feedbackType = %s)",
						FEEDBACK_TYPE_1.toString(), FEEDBACK_TYPE_2.toString(), FEEDBACK_TYPE_4.toString(), FEEDBACK_TYPE_10.toString());
			} else if (GROUP_FEEDBACK_TYPE_NONCUSTOMER.equals(theGroupFeedbackType)) {
				return String.format("(a.feedbackType = %s or a.feedbackType = %s or a.feedbackType = %s or a.feedbackType = %s or a.feedbackType = %s)",
						FEEDBACK_TYPE_3.toString(), FEEDBACK_TYPE_7.toString(), FEEDBACK_TYPE_8.toString(), FEEDBACK_TYPE_5.toString(), FEEDBACK_TYPE_12.toString());
			} else if (GROUP_FEEDBACK_TYPE_UNKNOWN.equals(theGroupFeedbackType)) {
				return String.format("(a.feedbackType = %s or a.feedbackType = %s or a.feedbackType is null)", FEEDBACK_TYPE_6.toString(), FEEDBACK_TYPE_9.toString());
			} else {
				return "";
			}
		}
	}

	@Column(length=500)
	public String getMarkup() {
		return markup;
	}

	public void setMarkup(String markup) {
		this.markup = markup;
	}
	
	public Integer getAgreementCount() {
		return agreementCount;
	}

	public void setAgreementCount(Integer agreementCount) {
		this.agreementCount = agreementCount;
	}

	public Integer getPublishCount() {
		return publishCount;
	}

	public void setPublishCount(Integer publishCount) {
		this.publishCount = publishCount;
	}

	public Integer getPictureCount() {
		return pictureCount;
	}

	public void setPictureCount(Integer pictureCount) {
		this.pictureCount = pictureCount;
	}

	public Boolean getBindBankCard() {
		return bindBankCard;
	}

	public void setBindBankCard(Boolean bindBankCard) {
		this.bindBankCard = bindBankCard;
	}
	
	public Integer getQrCodeCount() {
		return qrCodeCount;
	}

	public void setQrCodeCount(Integer qrCodeCount) {
		this.qrCodeCount = qrCodeCount;
	}

	@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	@LazyToOne(LazyToOneOption.PROXY)
	public LandlordFunction getLandlordFunction() {
		return landlordFunction;
	}

	public void setLandlordFunction(LandlordFunction landlordFunction) {
		this.landlordFunction = landlordFunction;
	}
}
